package com.cosmos.mapdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.Overlay;
import com.tianditu.android.maps.OverlayItem;

/**
 * Created by Leonardo on 2015/7/21.
 */
public class UserOverlay extends Overlay {
    private Context context = null;
    private OverlayItem item = null;
    private Paint paint = null;

    public UserOverlay(Context context) {
        this.context = context;
        this.paint = new Paint();
    }

    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {
       // 标记点击的坐标位置
        item = new OverlayItem(point, "Tap", point.toString());
        mapView.postInvalidate();
        return true;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);
        if (item == null)
            return;

        /*
        绘制点击位置及其坐标
         */
        Drawable arrow = context.getResources().getDrawable(R.drawable.tips_arrow);
        Point point = mapView.getProjection().toPixels(item.getPoint(), null);
        arrow.setBounds(point.x - arrow.getIntrinsicWidth() / 2,
                point.y - arrow.getIntrinsicHeight(),
                point.x + arrow.getIntrinsicWidth() / 2,
                point.y);
        arrow.draw(canvas);
        Rect bounds = new Rect();
        paint.getTextBounds(item.getSnippet(), 0, item.getSnippet().length() - 1, bounds);
       // 显示坐标文本
        canvas.drawText(item.getSnippet(), point.x - bounds.width() / 2,
                point.y - arrow.getIntrinsicHeight(), paint);
    }
}
